package com.example.pkmedical.Activity.warning;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.FinishActivityManager;
import com.example.pkmedical.Utils.SerialProtUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class HeadBloodWarningActivity extends BaseActivity {

    @BindView(R.id.weightheadblood_ok)
    Button weightheadbloodOk;
    @BindView(R.id.weightheadblood_cancle)
    Button weightheadbloodCancle;
    byte[] mBytes;
    @BindView(R.id.center_text)
    TextView centerText;
    private int state = 1;//:1前倾，2：后倾，3：boost

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_headbloodactivity);
        ButterKnife.bind(this);
        CommandUtil.IN_HEADBLOOD = true;
        FinishActivityManager.getManager().addActivity(this);
        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REMIND_MESSAGE);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
//        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REMIND_MESSAGE);
        switch (CommandUtil.DEFAULT_HEAD_TYPE) {
            case 1:
                state = 1;
                centerText.setText("");
                break;
            case 2:
                state = 2;
                centerText.setText("注意此动作可能会使头部充血");
                break;
            case 3:
                state = 3;
                centerText.setText("注意此动作可能会使头部充血");
                break;
            case 4:
                state = 4;
                centerText.setText("");
                break;
        }
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CommandUtil.IN_HEADBLOOD = false;
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        mBytes = event.getBytes();
        if (event.getCode() == MessageWrap.WARNING_CODE) {
            distinguish(mBytes);
        }
    }

    /**
     * 返回数据识别
     */
    private void distinguish(byte[] bytes) {
        if (DistinguishUtil.distinguishUtil(bytes)) {
            if (mHeadBloodTimeCountDisposable != null) {
                mHeadBloodTimeCountDisposable.dispose();
            }
            Log.d("yangshuohead", mHeadBloodTimeCountDisposable + "");
            switch (bytes[3]) {
                case 0x01://前倾
                    state = 1;
                    centerText.setText("");
                    SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REMIND_MESSAGE);
                    break;
                case 0x02://后倾
                    state = 2;
                    centerText.setText("注意此动作可能会使头部充血");
                    SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REMIND_MESSAGE);
                    break;
                case 0x03://bost
                    state = 3;
                    centerText.setText("注意此动作可能会使头部充血");
                    SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REMIND_MESSAGE);
                    break;
                case 0x04://一键移位
                    state = 4;
                    centerText.setText("");
                    SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REMIND_MESSAGE);
                    break;

            }
        }
    }

    @OnClick({R.id.weightheadblood_ok, R.id.weightheadblood_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.weightheadblood_ok:
                switch (state) {
                    case 1:
                        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.QIANQING_1);
                        TimeCount();
                        break;
                    case 2:
                        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.HOUQING_1);
                        TimeCount();
                        break;
                    case 3:
                        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.BOOST_1);
                        TimeCount();
                        break;
                    case 4:
                        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.CHAIR_1);
                        break;
                }
                HeadBloodWarningActivity.this.finish();
                break;
            case R.id.weightheadblood_cancle:
                SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.QIANQING_0);
                HeadBloodWarningActivity.this.finish();
                break;
        }
    }

    private int timeLong = 15;
    public static Disposable mHeadBloodTimeCountDisposable;

    /**
     * 倒计时
     */
    public void TimeCount() {
        if (mHeadBloodTimeCountDisposable != null) {
            //取消上一次的订阅
            mHeadBloodTimeCountDisposable.dispose();
        }
//        //0-count依次输出，延时0s执行，每1s发射一次。
        mHeadBloodTimeCountDisposable = Flowable.intervalRange(0,


                15, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> {
                    Log.d("yangshuo", timeLong + "");
                    timeLong -= 1;
                })
                .doOnComplete(() -> {
                    if (mHeadBloodTimeCountDisposable != null) {
                        mHeadBloodTimeCountDisposable.dispose();
                    }
                    switch (state) {
                        case 1:
                            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.QIANQING_0);
                            break;
                        case 2:
                            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.HOUQING_0);
                            break;
                        case 3:
                            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.BOOST_0);
                            break;
                    }

                })
                .subscribe();
    }
}
